import 'package:enjoy_plus_hm/utils/http.dart';
import 'package:enjoy_plus_hm/utils/toast.dart';
import 'package:flutter/material.dart';
import 'package:flutter_html/flutter_html.dart';

class NoticeDetail extends StatefulWidget {
  NoticeDetail({super.key});
  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  String data = '';
  Map<String, dynamic> params = {};
  Map notifyDetail = {};
  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    final data = ModalRoute.of(context)?.settings.arguments;
    if (data != null) {
      params = data as Map<String, dynamic>;
      getNoticeDetail();
    }
  }

  void getNoticeDetail() async {
    try {
      final res = await http.get('/announcement/${params['id']}');
      if (res['code'] != 10000) return ToastUtil.showError('请求失败');
      ToastUtil.showSuccess('请求成功');
      setState(() {
        notifyDetail = res['data'];
      });
    } catch (e) {
      ToastUtil.showError('请求失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('公告详情'),
        backgroundColor: Colors.transparent,
        centerTitle: true,
      ),
      body: ListView(
        children: [
          Container(
            child: Column(
              children: [
                // 标题
                Text(
                  notifyDetail['title'] ?? '',
                  style: const TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
                const SizedBox(height: 10),
                Column(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Text(
                      notifyDetail['creatorName'] ?? '',
                      style: const TextStyle(color: Colors.grey),
                    ),
                    Text(
                      notifyDetail['createdAt'] ?? '',
                      style: const TextStyle(color: Colors.grey),
                    ),
                  ],
                ),
                const SizedBox(height: 10),
                // 内容
                Html(data: notifyDetail['content'] ?? ''),
              ],
            ),
          ),
        ], //禁止用户手动滑动,避免滑动冲突
      ),
    );
  }
}
